package com.traveldream.backend.managed_beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Vector;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.traveldream.entities.DefaultPacket;
import com.traveldream.entities.Excursion;
import com.traveldream.entities.Flight;
import com.traveldream.entities.Hotel;
import com.traveldream.entities.Packet;
import com.traveldream.entities.Place;
import com.traveldream.entities.Product;
import com.traveldream.entity_managers.PacketManager;
import com.traveldream.entity_managers.ProductManager;


/**
 *	packet controller managed bean
 *		- actions for creating / editing packets
 *		- methods and attributes for creating/editing packet fields 
 */

@ManagedBean(name="packetBackend")
@SessionScoped
public class PacketBean {
	

	public static final String WEB_PATH = "/backend/packet/";
	
	
	@EJB
	private ProductManager prM; // instance of product manager
	
	@EJB
	private PacketManager pm; // instance of packet manager
	
	
	// option lists for form <select> tags
	private LinkedHashMap<String,Integer> flightListOptions    = new LinkedHashMap<String,Integer>();
	private LinkedHashMap<String,Integer> hotelListOptions     = new LinkedHashMap<String,Integer>();
	private LinkedHashMap<String,Integer> excursionListOptions = new LinkedHashMap<String,Integer>();
	
	private List list;  // packet list
	private Packet current; 	// current packet
	
// form fields
	private Excursion excursion;
	private Hotel hotel;
	private Flight flight;
	
	private int discount;
	

	// constructor
	public PacketBean()
	{
		current = new Packet();
		current.setPlace(new Place());
		discount=0;
		list = new ArrayList();
	}
	
	// actions
	
	/**
	 * first step of packet creation form
	 * useful for setting packet place and dates
	 * @return
	 */
	public String createActionStep1(){
		current = new Packet();
		current.setPlace(new Place());
		current.setName("");
		current.setStartingDate(new Date());
		current.setEndingDate(new Date());

		excursion = null;
		hotel = null;
		flight = null;
		return "new_step1";
	}
	
	/**
	 * second step of packet creation form
	 * force refresh to product list options searching them by place and dates
	 * @return
	 */
	public String createActionStep2(){
		return "new_step2";
	}
	
	/**
	 * third and last step of packet creation form
	 * 
	 * @return
	 */
	public String createActionStep3(){
		
		System.out.print("place id="+current.getPlace().getName());
		
		// create default packet and relate it to current
		DefaultPacket dPacket = new DefaultPacket();
		dPacket.setPacket(this.current);
		dPacket.setDiscount(this.discount);
		
		// initialize product list
		List<Product> productList = new ArrayList<Product>();
		
		// if user selected a flight, add it to list
		if(this.flight!=null)
		{
			productList.add(flight.getProduct());
			flight=null;
		}

		// if user selected an hotel, add it to list
		if(this.hotel!=null)
		{
			productList.add(hotel.getProduct());
			this.hotel=null;
		}

		// if user selected an excursion, add it to list
		if(this.excursion!=null)
		{
			System.out.print("Choosed excursion:"+excursion.getName());
			productList.add(excursion.getProduct());
			this.excursion = null;
		}	
		
		// product list size cannot be 0!!!
		if(productList.size()==0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("E' necessario scegliere almeno un prodotto!"));
			return "/backend/package/new_step2";
		}
		
		
		// set products to packet product list
		current.setProducts(productList);
		// call create(defaultPacket) from packet manager
		pm.create(dPacket);
		
		this.flight=null;
		this.hotel=null;
		this.excursion=null;
		discount = 0;
		list = pm.findAllDefaultPackets();
		
		return "list"; // go back to packet list page
	}
	
	
	/**
	 * first step of packet creation form
	 * useful for setting packet place and dates
	 * @return
	 */
	public String editActionStep1(Packet p){
		DefaultPacket dPacket = pm.findDefaultPacket(p.getId());
		current = dPacket.getPacket();
		current.setDefaultPacket(dPacket);
		discount = dPacket.getDiscount();

		excursion = null;
		hotel = null;
		flight = null;
		return "edit_step1";
	}
	
	/**
	 * second step of packet creation form
	 * force refresh to product list options searching them by place and dates
	 * @return
	 */
	public String editActionStep2(){
		return "edit_step2";
	}
	
	/**
	 * third and last step of packet creation form
	 * 
	 * @return
	 */
	public String editActionStep3(){
		
		System.out.print("place id="+current.getPlace().getName());
		
		
		
		// initialize product list
		List<Product> productList = new ArrayList<Product>();
		
		// if user selected a flight, add it to list
		if(this.flight!=null)
		{
			productList.add(flight.getProduct());
			flight=null;
		}

		// if user selected an hotel, add it to list
		if(this.hotel!=null)
		{
			productList.add(hotel.getProduct());
			this.hotel=null;
		}

		// if user selected an excursion, add it to list
		if(this.excursion!=null)
		{
			System.out.print("Choosed excursion:"+excursion.getName());
			productList.add(excursion.getProduct());
			this.excursion = null;
		}	
		
		// product list size cannot be 0!!!
		if(productList.size()==0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("E' necessario scegliere almeno un prodotto!"));
			return "/backend/package/edit_step2";
		}
		
		
		// set products to packet product list
		current.setProducts(productList);
		DefaultPacket dPacket = current.getDefaultPacket();
		dPacket.setDiscount(this.discount);
		// call update(defaultPacket) from packet manager
		pm.update(dPacket);
		
		this.flight=null;
		this.hotel=null;
		this.excursion=null;
		discount = 0;
		list = pm.findAllDefaultPackets();
		
		return "list"; // go back to packet list page
	}
	
	
	
	
	// getters and setters
	
	/**
	 * query the DB searching flights compatible with
	 * user choices on dates and place
	 * @return
	 */
	public List<Flight> getFlightList(){
		List<Flight> list = prM.findFlightByDatesAndPlace(current.getStartingDate(),current.getEndingDate(),current.getPlace());
		return list;
	}
	/**
	 * query the DB searching hotels compatible with
	 * user choices on dates and place
	 * @return
	 */
	public List<Hotel> getHotelList(){
		List<Hotel> list = prM.findHotelByDatesAndPlace(current.getStartingDate(),current.getEndingDate(),current.getPlace());
		return list;
	}

	/**
	 * query the DB searching excursions compatible with
	 * user choices on dates and place
	 * @return
	 */
	public List<Excursion> getExcursionList(){
		//List<Excursion> list = prM.findAllExcursions();
		List<Excursion> list = prM.findExcursionByDatesAndPlace(current.getStartingDate(),current.getEndingDate(),current.getPlace());
		return list;
	}

	
	
	public Packet getCurrent()
	{
		return this.current;
	}
	
	public void setCurrent(Packet current)
	{
		this.current = current;
	}

	public List getList() {
		if( list.size()==0)
			list = pm.findAllDefaultPackets();
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Excursion getExcursion() {
		return excursion;
	}

	public void setExcursion(Excursion excursion) {
		this.excursion = excursion;
	}

	public Hotel getHotel() {
		return hotel;
	}

	public void setHotel(Hotel hotel) {
		this.hotel = hotel;
	}

	public Flight getFlight() {
		return flight;
	}

	public void setFlight(Flight flight) {
		this.flight = flight;
	}
	
	public int getDiscount()
	{
		return this.discount;
	}
	
	public void setDiscount(int discount)
	{
		this.discount = discount;
	}
	
	
	
}
